// 计算选中的文本长度
import { computed } from "vue"
import { useEditorStore } from "@/stores/editStore"
import { storeToRefs } from "pinia"

const editorStore = useEditorStore()
const {editorInstance} = storeToRefs(editorStore)
const selectedTextLength = computed(() => {

  if (!editorInstance.value) return 0
  const { from, to } = editorInstance.value.state.selection
  return to - from
})
const selectedText = computed(()=>{
  if (!editorInstance.value)return ''
  const {state} = editorInstance.value
  const {from,to} = state.selection
  return state.doc.textBetween(from,to,'')
})


// 控制气泡菜单显示条件
const shouldShowBubble = ({  }) => {
    console.log(editorInstance.value)
  const { from, to } = editorInstance.value.state.selection
  return from !== to // 只在有文本选中时显示
}

// Tippy 配置
const tippyOptions = {
  interactive: true, // 禁用交互
  placement: 'top',
  appendTo: () => document.body,
}
export {tippyOptions ,shouldShowBubble,selectedTextLength,selectedText}